package me.firstapp.repository.zerone.cqssc.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.zerone.cqssc.CqsscPlan;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.zerone.cqssc.CqsscPlanRepository;

@Repository
public class CqsscPlanRepositoryImpl extends EntityRepositoryImpl<CqsscPlan> implements CqsscPlanRepository {

	@Override
	protected Class<CqsscPlan> getPersistentClass() {
		return CqsscPlan.class;
	}

	@Override
	public Page<CqsscPlan> findPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		StringBuilder sql = new StringBuilder(" FROM CqsscPlan bean WHERE 1=1");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					sql.append(" AND bean." + key + " LIKE :value ");
					parameters.put("value", "%" + value + "%");
				}
			}
		}

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(CqsscPlan.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public List<CqsscPlan> findByPlanIssueNo(String planIssueNo) {
		StringBuilder sql = new StringBuilder(" FROM CqsscPlan bean WHERE 1=1");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.planIssueNo =:planIssueNo ");
		parameters.put("planIssueNo", planIssueNo);

		return super.query(CqsscPlan.class, sql.toString(), parameters);
	}

	@Override
	public List<CqsscPlan> findByPlanIssueNoAndWeiAndStartPlanNum(String planIssueNo, String wei) {
		StringBuilder sql = new StringBuilder(" FROM CqsscPlan bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.planIssueNo =:planIssueNo ");
		parameters.put("planIssueNo", planIssueNo);

		sql.append(" AND bean.wei =:wei ");
		parameters.put("wei", wei);

		return super.query(CqsscPlan.class, sql.toString(), parameters);
	}

	@Override
	public List<CqsscPlan> findByPlanIssueDate(String planIssueDate) {
		StringBuilder sql = new StringBuilder(" FROM CqsscPlan bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.planIssueDate =:planIssueDate ");
		parameters.put("planIssueDate", planIssueDate);

		return super.query(CqsscPlan.class, sql.toString(), parameters);
	}

	@Override
	public List<CqsscPlan> findPlanCqsscPlansByPlanIssueNo(String planIssueNo) {
		StringBuilder sql = new StringBuilder(" FROM CqsscPlan bean WHERE 1=1");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.planIssueNo =:planIssueNo ");
		parameters.put("planIssueNo", planIssueNo);

		sql.append(" AND bean.status <>:status ");
		parameters.put("status", CqsscPlan.CQSSC_PLAN_STATUS_NOT_PLAN);

		return super.query(CqsscPlan.class, sql.toString(), parameters);
	}

}
